package com.hyt.it.ogt.kq.main.controller.gov;


import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.common.gov.constant.CodeEnum;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.model.vo.CandidateAreaListVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.Tree;
import com.hyt.it.ogt.kq.service.gov.service.ITaskCandidateAreaService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 任务考生所属区域表 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-07-26
 */

@Slf4j
@RestController
@Api(tags = "128.考生区域接口", value = "考生区域接口")
@ApiSort(value = 128)
@RequestMapping("/kw/gov/candidateArea")
public class TaskCandidateAreaController {

    @Resource
    private ITaskCandidateAreaService iTaskCandidateAreaService;

    @ApiOperation(
            value = "28.1 获取考生省级区域下拉列表",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskId",
                    value = "考试任务id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/get/provinceList")
    public ApiResponse<List<CandidateAreaListVO>> getProvinceList(@RequestParam String taskId) {
        log.info("# 获取考生省级区域下拉列表参数: {}", taskId);
        try {
            List<CandidateAreaListVO> candidateAreaListVOList = iTaskCandidateAreaService.getAreaList(taskId,
                    CodeEnum.AREA_TYPE_PROVINCE.getCode(), null);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(candidateAreaListVOList)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生省级区域下拉列表失败", e);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.GET_PROVINCE_LIST_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "28.2 获取考生市级区域下拉列表",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskId",
                    value = "考试任务id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            ),
            @ApiImplicitParam(
                    name = "parentId",
                    value = "父级区域id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/get/cityList")
    public ApiResponse<List<CandidateAreaListVO>> getCityList(@RequestParam String taskId,
                                                              @RequestParam String parentId) {
        log.info("# 获取考生市级区域下拉列表参数: {}, {}", taskId, parentId);
        try {
            List<CandidateAreaListVO> candidateAreaListVOList = iTaskCandidateAreaService.getAreaList(taskId,
                    CodeEnum.AREA_TYPE_CITY.getCode(), parentId);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(candidateAreaListVOList)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生市级区域下拉列表失败", e);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.GET_CITY_LIST_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "28.3 获取考生区县级区域下拉列表",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskId",
                    value = "考试任务id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            ),
            @ApiImplicitParam(
                    name = "parentId",
                    value = "父级区域id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/get/districtList")
    public ApiResponse<List<CandidateAreaListVO>> getDistrictList(@RequestParam String taskId, @RequestParam String parentId) {
        log.info("# 获取考生区县级区域下拉列表参数: {}, {}", taskId, parentId);
        try {
            List<CandidateAreaListVO> candidateAreaListVOList = iTaskCandidateAreaService.getAreaList(taskId,
                    CodeEnum.AREA_TYPE_DISTRICT.getCode(), parentId);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(candidateAreaListVOList)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生区县级区域下拉列表失败", e);
            return ApiResponse.<List<CandidateAreaListVO>>builder()
                    .code(ResponseCode.GET_DISTRICT_LIST_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "28.4 获取考生区域树",
            notes = "20000:成功"
    )
    @GetMapping("/allTree")
    public ApiResponse<List<Tree>> allTree(@RequestParam String taskId,@RequestParam(required = false) String type) {
        log.info("# 获取考生区域树: ");
        try {
            List<Tree> candidateAreaListVOList = iTaskCandidateAreaService.allTree(taskId,type);
            return ApiResponse.<List<Tree>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(candidateAreaListVOList)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生区县级区域下拉列表失败", e);
            return ApiResponse.<List<Tree>>builder()
                    .code(ResponseCode.GET_DISTRICT_LIST_EXCEPTION.getCode())
                    .build();
        }
    }

}
